{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业二 \n",
    "\n",
    "每行第一字母表示本人，其他字母他的朋友，找出有共同朋友的人，和共同朋友是谁，如何使用MapReduce来实现 20分\n",
    "\n",
    "```\n",
    "A B C D E F\n",
    "B A C D E\n",
    "C A B E\n",
    "D A B E\n",
    "E A B C D\n",
    "F A\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "friends = []\n",
    "\n",
    "with open(\"./friends.txt\", \"r\") as f:  # 打开文件\n",
    "    datas = f.read()  # 读取文件\n",
    "    # 所有行数据 用 \\n 换行符拆分\n",
    "    lines = datas.strip().split('\\n')\n",
    "    \n",
    "    # 把每行数据拆分\n",
    "    for line in lines:\n",
    "        friends.append(line.strip().split(' '))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['A', 'B', 'C', 'D', 'E', 'F'],\n",
       " ['B', 'A', 'C', 'D', 'E'],\n",
       " ['C', 'A', 'B', 'E'],\n",
       " ['D', 'A', 'B', 'E'],\n",
       " ['E', 'A', 'B', 'C', 'D'],\n",
       " ['F', 'A']]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数据\n",
    "friends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def friend_map(friends):    \n",
    "    map_data = []\n",
    "    for friend in friends:\n",
    "        # 自己\n",
    "        owner = friend[0]\n",
    "        # 自己的朋友们\n",
    "        owner_friends = friend[1:]\n",
    "        \n",
    "        # 共同的朋友 \n",
    "        for i in range(0, len(owner_friends)):\n",
    "            for j in range(i + 1, len(owner_friends)):\n",
    "                map_data.append(owner_friends[i] + ' ' + owner_friends[j] + ' ' + owner)\n",
    "                \n",
    "    return map_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B C A',\n",
       " 'B D A',\n",
       " 'B E A',\n",
       " 'B F A',\n",
       " 'C D A',\n",
       " 'C E A',\n",
       " 'C F A',\n",
       " 'D E A',\n",
       " 'D F A',\n",
       " 'E F A',\n",
       " 'A C B',\n",
       " 'A D B',\n",
       " 'A E B',\n",
       " 'C D B',\n",
       " 'C E B',\n",
       " 'D E B',\n",
       " 'A B C',\n",
       " 'A E C',\n",
       " 'B E C',\n",
       " 'A B D',\n",
       " 'A E D',\n",
       " 'B E D',\n",
       " 'A B E',\n",
       " 'A C E',\n",
       " 'A D E',\n",
       " 'B C E',\n",
       " 'B D E',\n",
       " 'C D E']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看结果集\n",
    "map_data = friend_map(friends)\n",
    "map_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reduce 函数\n",
    "def friend_reduce(friends):\n",
    "    word_dict = {}\n",
    "    \n",
    "    for line in friends:\n",
    "        # 自己\n",
    "        line_info = line.strip().split(' ')\n",
    "        # 排序\n",
    "        line_info.sort()\n",
    "        # 组成升序 key\n",
    "        line_key = ' '.join(line_info)\n",
    "\n",
    "        # 统计次数\n",
    "        if line_key not in word_dict.keys(): \n",
    "            word_dict[line_key] = 1\n",
    "        else:\n",
    "            word_dict[line_key] += 1\n",
    "            \n",
    "    for j in word_dict:\n",
    "        print(j, word_dict[j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A B C 3\n",
      "A B D 3\n",
      "A B E 3\n",
      "A B F 1\n",
      "A C D 1\n",
      "A C E 3\n",
      "A C F 1\n",
      "A D E 3\n",
      "A D F 1\n",
      "A E F 1\n",
      "B C D 1\n",
      "B C E 3\n",
      "B D E 3\n",
      "C D E 1\n"
     ]
    }
   ],
   "source": [
    "# 结果\n",
    "friend_reduce(map_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
